#include "VideoSummary.h"

VideoSummary::VideoSummary(void)
{
}


VideoSummary::~VideoSummary(void)
{
}

int VideoSummary::initImg( Mat& img )
{
	//resize(img , img , Size(352,288) );
	GaussianBlur( img ,img , Size(13,13) , 0 , 0);
	return 0;
}

int VideoSummary::dilateIMG( Mat &img , int winSize )
{
	Mat element = getStructuringElement(MORPH_ELLIPSE , Size( winSize , winSize) );
	dilate( img , img , element );
	namedWindow("Mask",0);
	imshow("Mask", img );
	return 0;
}

int VideoSummary::getContour( Mat & img , Mat &outImg)
{
	vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	RNG rng(12345);
	findContours( img , contours , hierarchy , CV_RETR_TREE , CV_CHAIN_APPROX_NONE , Point(0,0));
	vector<vector<Point> >hull( contours.size() );
	vector<Point2f>center( contours.size() );
	vector<float>radius( contours.size() );
	vector<vector<Point> > contours_poly( contours.size() );
	for( int i = 0; i < contours.size(); i++ )
	{
		approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); 
		convexHull( Mat(contours[i]), hull[i], false ); 
		minEnclosingCircle( contours_poly[i], center[i], radius[i] );
	}

	Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
	outImg.copyTo( drawing , img );
	for( int i = 0; i< contours.size(); i++ )
	{ 
		Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
		drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() ); 
		drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
		circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
	}
	string contourInfo = format( "hull: %d",hull.size() );
	putText( drawing , contourInfo , Point( 20 , 40 ) , FONT_HERSHEY_PLAIN , 2, RGB(0,0,255), 2 );
	namedWindow("contour" , 0);
	imshow("contour", drawing);
	return hull.size();
}
